package com.wangp.mywrite.s_netty.netty.ch2;

import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.DefaultPromise;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;

/**
 * @author tooStronger
 * @date 2023/3/18
 */
@Slf4j
public class TestNettyPromise {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 1. 配备eventLoop
        EventLoop eventLoop = new NioEventLoopGroup().next();
        // 2. 主动创建 promise
        DefaultPromise<Integer> promise = new DefaultPromise<>(eventLoop);

        // 3. 任意线程执行完毕向promise填充结果
        new Thread(() -> {
            try {
                log.info("开始计算..");
                int i = 1/0;
                Thread.sleep(1000);
                // 填充成功结果
                promise.setSuccess(80);
            } catch (Exception e) {
                e.printStackTrace();
                promise.setFailure(e);
            }

        }).start();

        log.info("等待结果，");
        log.info("结果是{}", promise.get());
    }
}
